﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using LinFu.IoC.Configuration;

namespace CoreEx.DynamicInvoker.Implementation
{
    [Implements(typeof(IDynamicFactory<ConstructorInfo>), LifecycleType.OncePerRequest)]
    public class ConstructorFactory : DynamicFactoryBase<ConstructorInfo>
    {
        protected override void LoadInstance(ILGenerator iLGenerator, ConstructorInfo targetMember)
        {
            //do nothing
        }

        protected override void EmitMethodBody(ILGenerator ilGenerator, ConstructorInfo target)
        {
            ilGenerator.Emit(OpCodes.Newobj, target);
        }

        public override IEnumerable<Type> GetParameters(ConstructorInfo member)
        {
            return member.GetParameters().Select(p => p.ParameterType);            
        }

        public override Type GetReturnType(ConstructorInfo member)
        {            
            return typeof (void);
            }        
    }
}
